// UART_commundate_indexcatdate_indexon.date_indexno
int LED = 13;
byte  Buffer[3] = {0x00,0x00,0x00};
void Communication_Decode(void)
{
    if(Buffer[0]==0x00)
    {
        switch(Buffer[1])
        {
            case 0x01:digitalWrite(LED, 1);return;
            case 0x02:digitalWrite(LED, 0);return;
            case 0x03:digitalWrite(LED, 1);return;
            case 0x04:return;
            case 0x00:return;
            case 0x05:return;
            case 0x06:return;
            case 0x07:return;
            case 0x08:return;
            default: return;
        }
    }
    else if(Buffer[0]==0x01)
    {
        if(Buffer[2]>180)
            return;
        switch(Buffer[1])
        {
            case 0x01:return;
            case 0x02:return;
            case 0x03:return;
            case 0x04:return;
            case 0x05: return;
            default : return;
        }
    }
    else if(Buffer[0]==0x02)
    {
        if(Buffer[2]>10)
            return;
        switch(Buffer[1])
        {
            case 0x01:return;
            case 0x02:return;
            default : return;
        }
    }
    else
    {
        return;
    }
}
int Transmit_byte[5] = {0xFF,00,00,00,0xFF};
void Transmit_variable(int Type,float Value)
{
  Value = Value * 100;
  Transmit_byte[1] = Type;
  Transmit_byte[2] = int(Value)/100;
  Transmit_byte[3] = int(Value)%100;
  for(int i=0;i < 5;i++)
  {
     Serial.write(Transmit_byte[i]);
  }
}
void setup() {
    Serial.begin(115200);
    Serial.flush();
    pinMode(LED, OUTPUT);
}

byte SBUF= 0x00;
int rec_flag = 0; //等于0等待接受 等于1正在接受
int index=0;
void loop()
{
   if (Serial.available()>0) //如果Arduino控制板的读缓冲区中存在上位机下达的字节
   {
      SBUF = Serial.read();
      //delay(2);
      //Serial.print(SBUF,HEX);
      if(SBUF!= -1)
      {
        if(rec_flag==0)
        {
      	  if(SBUF==0xff)
          {
      	      rec_flag=1;
      	      index=0;
           }
         }
         else
         {
            if(SBUF==0xff)
            {
            	rec_flag=0;
            	if(index==3)
            	{
            	  Communication_Decode();
                }
                index=0;
            }
            else
             {
                 Buffer[index]=SBUF;
                 //Serial.print(Buffer[index],HEX);
                 index++;
              }
      	  }
      }
   }
   
    Transmit_variable(0x03,50.20);
    Transmit_variable(0x04,41.66);
}
